<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.keyword"
        placeholder="请输入关键词"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter="handleFilter"
      />
      <el-select
        v-model="listQuery.type"
        placeholder="任务类型"
        clearable
        class="filter-item"
        style="width: 130px"
      >
        <el-option label="开发任务" value="development" />
        <el-option label="测试任务" value="testing" />
        <el-option label="文档任务" value="documentation" />
        <el-option label="其他任务" value="other" />
      </el-select>
      <el-select
        v-model="listQuery.priority"
        placeholder="优先级"
        clearable
        class="filter-item"
        style="width: 130px"
      >
        <el-option label="高" value="high" />
        <el-option label="中" value="medium" />
        <el-option label="低" value="low" />
      </el-select>
      <el-select
        v-model="listQuery.status"
        placeholder="任务状态"
        clearable
        class="filter-item"
        style="width: 130px"
      >
        <el-option label="待处理" value="pending" />
        <el-option label="进行中" value="processing" />
        <el-option label="已完成" value="completed" />
        <el-option label="已取消" value="cancelled" />
      </el-select>
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >
        搜索
      </el-button>
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-plus"
        @click="handleCreate"
      >
        新建任务
      </el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="任务标题" prop="title" align="center" min-width="200">
        <template #default="{row}">
          <router-link :to="'/task/detail/'+row.id" class="link-type">
            <span>{{ row.title }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="任务类型" prop="type" align="center" width="100">
        <template #default="{row}">
          <el-tag :type="row.type === 'development' ? 'primary' : row.type === 'testing' ? 'success' : 'info'">
            {{ row.type === 'development' ? '开发' : row.type === 'testing' ? '测试' : '其他' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="优先级" prop="priority" align="center" width="80">
        <template #default="{row}">
          <el-tag :type="row.priority === 'high' ? 'danger' : row.priority === 'medium' ? 'warning' : 'info'">
            {{ row.priority === 'high' ? '高' : row.priority === 'medium' ? '中' : '低' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="负责人" prop="assignee" align="center" width="100" />
      <el-table-column label="开始日期" prop="startDate" align="center" width="120" />
      <el-table-column label="截止日期" prop="endDate" align="center" width="120" />
      <el-table-column label="状态" prop="status" align="center" width="100">
        <template #default="{row}">
          <el-tag :type="row.status === 'completed' ? 'success' : row.status === 'processing' ? 'primary' : 'info'">
            {{ row.status === 'pending' ? '待处理' : row.status === 'processing' ? '进行中' : row.status === 'completed' ? '已完成' : '已取消' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200">
        <template #default="{row}">
          <el-button
            size="small"
            type="primary"
            @click="handleUpdate(row)"
          >
            编辑
          </el-button>
          <el-button
            size="small"
            type="danger"
            @click="handleDelete(row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'

const router = useRouter()
const listLoading = ref(false)
const list = ref([])
const total = ref(0)

const listQuery = ref({
  page: 1,
  limit: 20,
  keyword: '',
  type: '',
  priority: '',
  status: ''
})

const getList = async () => {
  listLoading.value = true
  try {
    // TODO: 调用API获取任务列表
    listLoading.value = false
  } catch (error) {
    console.error('获取任务列表失败:', error)
    listLoading.value = false
  }
}

const handleFilter = () => {
  listQuery.value.page = 1
  getList()
}

const handleCreate = () => {
  router.push('/task/create')
}

const handleUpdate = (row) => {
  router.push(`/task/edit/${row.id}`)
}

const handleDelete = (row) => {
  ElMessageBox.confirm('确认要删除该任务吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    try {
      // TODO: 调用API删除任务
      ElMessage.success('删除成功')
      getList()
    } catch (error) {
      console.error('删除任务失败:', error)
    }
  }).catch(() => {
    // 取消删除
  })
}

// 初始化加载数据
getList()
</script>

<style lang="scss" scoped>
.filter-container {
  padding-bottom: 10px;

  .filter-item {
    display: inline-block;
    vertical-align: middle;
    margin-bottom: 10px;
    margin-right: 10px;
  }
}

.link-type {
  color: #409EFF;
  text-decoration: none;

  &:hover {
    color: #66b1ff;
  }
}
</style>